package com.example;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.nio.charset.StandardCharsets;

public class PasswordHashing {

    public static void main(String[] args) {
        try {
            String password = "secret";
            String salt = "U9i%=N+m]#i9yvUV:bA/3n4X9JdPXf=n";
            
            // 将盐值和密码拼接
            String passwordWithSalt = salt + password; // 或者 password + salt，根据具体实现
            
            // 获取 SHA-256 实例
            MessageDigest digest = MessageDigest.getInstance("SHA-256");
            
            // 计算哈希值
            byte[] hashBytes = digest.digest(passwordWithSalt.getBytes(StandardCharsets.UTF_8));
            
            // 将字节数组转换为十六进制字符串
            StringBuilder hexString = new StringBuilder();
            for (byte b : hashBytes) {
                hexString.append(String.format("%02x", b));
            }
            
            // 输出哈希值
            String passwordHash = hexString.toString();
            System.out.println("Generated password hash: " + passwordHash);
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        }
    }
}
